package com.qyhl.school.school.column.course.detail;

import android.annotation.SuppressLint;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.school.R;
import com.qyhl.school.R2;
import com.qyhl.school.school.column.course.detail.introducation.SchoolColumnCourseIntroductionFragment;
import com.qyhl.school.school.column.course.detail.live.SchoolColumnCourseLiveFragment;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.school.SchoolCourseDetailBean;
import com.qyhl.webtv.commonlib.utils.MShareBoard;
import com.qyhl.webtv.commonlib.utils.view.video.detailvideo.QYVideoPlayer;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.weavey.loading.lib.LoadingLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.SCHOOL_COLUMN_COURSE_DETAIL)
public class SchoolColumnCourseDetailActivity extends BaseActivity implements SchoolColumnCourseDetailContract.SchoolColumnCourseDetailView {
    @BindView(R2.id.video_player)
    QYVideoPlayer videoPlayer;
    @BindView(R2.id.pro_title)
    TextView proTitle;
    @BindView(R2.id.hit_num)
    TextView hitNum;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;

    private SchoolColumnCourseDetailPresenter mPresenter;

    private List<SchoolCourseDetailBean.CourseItem> mDataList = new ArrayList<>();

    private int albumId;

    private SchoolCourseDetailBean mBean;

    @Override
    protected int getContentView() {
        return R.layout.activity_school_column_course_detail;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.statusBarColor(R.color.black).statusBarDarkFont(false).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);
        albumId = getIntent().getIntExtra("albumId", 0);
        mPresenter = new SchoolColumnCourseDetailPresenter(this);

        mPresenter.getDetail(albumId);
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getDetail(albumId);
            }
        });

    }

    public void videoPlay(String url) {
        if (videoPlayer == null) {
            return;
        }
        videoPlayer.setUp(url == null ? "" : url, false, "");
        videoPlayer.getStartButton().performClick();//自动播放
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setDetail(SchoolCourseDetailBean bean) {
        mBean = bean;
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        proTitle.setText(bean.getTitle());

        hitNum.setText(bean.getViewCount() + "人观看");

        if (mBean.getCourseList() != null && mBean.getCourseList().size() > 0) {
            mBean.getCourseList().get(mBean.getCourseList().size() - 1).setSelected(true);
        }


        //设置封面图
        ImageView imageView = new ImageView(this);
        Glide.with(this)
                .load(bean.getCover())
                .apply(new RequestOptions().centerCrop().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default))
                .into(imageView);
        videoPlayer.setThumbImageView(imageView);

        videoPlayer.setRotateViewAuto(false);
        videoPlayer.setLockLand(true);
        String url = "";
        if (bean.getCourseList() != null && bean.getCourseList().size() > 0) {
            videoPlayer.setPlayStatusVisible(true);
            url = bean.getCourseList().get(bean.getCourseList().size() - 1).getUrl();
            videoPlayer.setUp(url, true, "");
        } else {
            videoPlayer.setUp(url, true, "");
            videoPlayer.setPlayStatusVisible(false);
        }

        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
                videoPlayer.startWindowFullscreen(SchoolColumnCourseDetailActivity.this, true, true);
            }
        });


        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!GSYVideoManager.backFromWindowFull(SchoolColumnCourseDetailActivity.this)) {
                    finish();
                }
            }
        });

        List<String> nameList = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        nameList.add("课程");
        nameList.add("介绍");

        fragments.add(SchoolColumnCourseLiveFragment.newInstance(mBean.getCourseList()));
        fragments.add(SchoolColumnCourseIntroductionFragment.newInstance(bean.getInfo()));

        viewPager.setAdapter(new Adapter(getSupportFragmentManager(), fragments, nameList));
        viewPager.setOffscreenPageLimit(fragments.size());
        tabLayout.setViewPager(viewPager);


    }

    @Override
    public void setError(String msg) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Error);
        if (!NetUtil.isNetworkAvalible(this)) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }


    @OnClick(R2.id.share_btn)
    public void onClick() {
        MShareBoard shareBoard = new MShareBoard(SchoolColumnCourseDetailActivity.this, mBean.getId() + "", mBean.getTitle(), mBean.getCover(), "", 32, false);
        shareBoard.showPopupWindow();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (QYVideoPlayer.getPlayMode() == QYVideoPlayer.PlayMode.LIVE) {
            if (videoPlayer.isIfCurrentIsFullscreen()) {
                videoPlayer.getStartButton().performClick();
            } else {
                videoPlayer.startPlayLogic();
            }
        } else {
            videoPlayer.onVideoResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        videoPlayer.onVideoPause();
    }

    @Override
    public void onBackPressed() {
        if (GSYVideoManager.backFromWindowFull(this)) {
            return;
        }
        super.onBackPressed();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoPlayer.release();
    }


    static class Adapter extends FragmentPagerAdapter {
        private List<Fragment> fragmentList;
        private List<String> names;

        Adapter(FragmentManager fm, List<Fragment> fragmentList, List<String> names) {
            super(fm);
            this.fragmentList = fragmentList;
            this.names = names;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return names.get(position);
        }
    }

}
